package com.itheima.sky.service.impl;

import com.itheima.sky.dao.AddressBookMapper;
import com.itheima.sky.pojo.AddressBook;
import com.itheima.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description AddressBookServiceImpl
 * @Author huangbailin
 * @Date 2024-06-27
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 查询默认地址
     *
     * @param currentId
     * @return
     */
    @Override
    public AddressBook getDefaultAddress(Long currentId) {
        return addressBookMapper.getDefaultAddress(currentId);
    }

    /**
     * 删除地址
     *
     * @param id
     */
    @Override
    public void delete(BigDecimal id) {
        addressBookMapper.delete(id);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     */
    @Override
    public void edit(AddressBook addressBook) {
        addressBookMapper.edit(addressBook);
    }

    /**
     * 更新默认地址
     *
     * @param addressBook
     */
    @Override
    public void updateDefaultAddress(AddressBook addressBook) {
        //1. 把该用户地址全部修改为非默认
        addressBookMapper.removeDefaultAddress(addressBook.getUserId());
        //2 重新设置当前地址为默认地址
        addressBookMapper.updateDefaultAddress(addressBook);
    }

    /**
     * 查询地址列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<AddressBook> queryAddressList(Long userId) {

        List<AddressBook> addressBookList = addressBookMapper.queryAddressList(userId);
        return addressBookList;
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.getById(id);
    }

    /**
     * 保存地址
     *
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {

//        加了这一行
//        addressBook.setUserId(1L);
//        addressBook.setCreateUser(1l);
//        addressBook.setUpdateUser(1l);
        addressBookMapper.save(addressBook);
    }
}
